package com.yidingdui.config;

import com.yidingdui.interceptors.UserInfoInterceptor;
import com.yidingdui.interceptors.LoginInterceptor;
import com.yidingdui.util.JwtTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class MVCConfig implements WebMvcConfigurer {
    @Autowired
    private JwtTool jwtTool;
    //解决跨域问题
    @Override
    public void addCorsMappings(org.springframework.web.servlet.config.annotation.CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8079")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInfoInterceptor());
        registry.addInterceptor(new LoginInterceptor(jwtTool))
                .addPathPatterns("/**")
                .excludePathPatterns("/users/login", "/users/register", "/error");
    }
}
